from apps.members.models import Member
from apps.goods.models import Goods
from exts import db
from apps.common.base_model import BaseModel

class Cart(BaseModel):
    __tablename__="t_cart"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    user =db.Column(db.Integer,db.ForeignKey("t_member.id"))
    goods =db.Column(db.Integer,db.ForeignKey("t_goods.id"))
    goods_num =db.Column(db.Integer, default=1, comment='购物车中商品数量')

    def __repr__(self):
        return f"{self.goods.id}"


class Order(BaseModel):
    __tablename__="t_order"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    order_sn = db.Column(db.String(50), nullable=True, comment="订单号")
    order_total = db.Column(db.Integer,default=0,comment="商品总件数")
    order_price =db.Column(db.DECIMAL(5,2),default=0,comment="订单总金额")
    address = db.Column(db.String(100), default="", comment="收货地址")
    contact_name =db.Column(db.String(20),default="", comment="联系人")
    contact_mobile =db.Column(db.String(11), default="",comment="联系电话")
    pay_method =db.Column(db.Integer,default=0, comment="支付方式")
    memo =db.Column(db.String(200),default='', comment="订单备注")
    order_state =db.Column(db.Enum("paying","payed","shipping","complete","cancel"),default='paying',comment="用户")
    user_id =db.Column(db.Integer,db.ForeignKey("t_member.id"))
    # create_date =db.Column(db.DateTime,default=datetime.now, comment='创建时间')

class OrderGoods(BaseModel):
    '''
    订单商品关联表
    '''
    __tablename__="t_order_goods"
    id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    order_id =db.Column(db.Integer,db.ForeignKey("t_order.id"))
    goods_id =db.Column(db.Integer,db.ForeignKey("t_goods.id"))
    goods_num =db.Column(db.Integer, default=1, comment='购物车中商品数量')
    #保留下订单时的价格快照，因为会发生变化
    price = db.Column(db.Float,comment="商品价格")
    # create_date =db.Column(db.DateTime,default=datetime.now, comment='创建时间')